<template>
  <!--          Information dialogue     *************************************** -->
  <el-dialog
    :title="InformationRemark.title"
    :visible="isVisible"
    :before-close="hide"
  >
    <el-form
      ref="InformationValue"
      :model="InformationValue"
      label-width="120px"
    >
      <el-form-item :label="InformationRemark.username">
        <el-input v-model="InformationValue.userVo.username" />
      </el-form-item>
      <el-form-item :label="InformationRemark.phoneNumber">
        <el-input v-model="InformationValue.userVo.phoneNumber" />
      </el-form-item>
      <el-form-item :label="InformationRemark.sex">
        <el-input v-model="InformationValue.userVo.sex" />
      </el-form-item>
      <el-form-item :label="InformationRemark.age">
        <el-input v-model="InformationValue.userVo.age" />
      </el-form-item>
      <el-form-item :label="InformationRemark.createdTime">
        <el-input v-model="InformationValue.userVo.createdTime" />
      </el-form-item>
      <el-form-item :label="InformationRemark.prover">
        <el-select
          v-model="InformationValue.approverId"
          :placeholder="InformationRemark.proverOptions[0].label"
          disabled
        >
          <el-option
            v-for="item in InformationRemark.proverOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="InformationRemark.type">
        <el-select
          v-model="InformationValue.type"
          :placeholder="InformationValue.loanVo.type"
          disabled
        >
          <el-option
            v-for="item in InformationRemark.typeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-button
          v-if="InformationValue.loanVo.type == 'MortgageLoan'"
          @click="download"
          >下载附件</el-button
        >
      </el-form-item>
      <el-form-item :label="InformationRemark.totalMoney">
        <el-input v-model="InformationValue.loanVo.totalMoney" />
      </el-form-item>
      <el-form-item :label="InformationRemark.interestRate">
        <el-input v-model="InformationValue.loanVo.interestRate" />
      </el-form-item>
      <el-form-item>
        <!-- <el-button type="primary" @click="Information">创建</el-button> -->
        <el-button @click="hide">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import request from "@/utils/request";

export default {
  props: {
    isVisible: {
      type: Boolean,
      required: false,
      default: false,
    },
    InformationValue: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      //   InformationValue: {},
      InformationRemark: {
        title: "贷款详细信息",
        username: "姓名",
        phoneNumber: "手机号",
        sex: "性别",
        age: "年龄",
        createdTime: "账号创建时间",
        prover: "贷款人",
        proverOptions: [
          {
            label: "银行",
            value: "1234",
          },
          {
            label: "其他人",
            value: "1234567",
          },
        ],
        type: "贷款类型",
        typeOptions: [
          {
            label: "信用贷",
            value: "CreditLoan",
          },
          {
            label: "抵押物贷款",
            value: "MortgageLoan",
          },
        ],
        totalMoney: "总额",
        interestRate: "利率",
        createdTime: "贷款时间",
      },
    };
  },
  created() {},
  methods: {
    Information() {
      this.$store.dispatch("request/post", this.InformationValue).then(() => {
        this.$message.success("请求贷款成功!!");
        this.$emit("updateData");
      });
    },
    hide() {
      this.$emit("hide");
    },
    download() {
      request({
        url: "/basic/file/download",
        method: "get",
        params: {
          sourceId: this.InformationValue.loanVo.id,
        },
        responseType: "blob",
      }).then((response) => {
        if (response.data.size == 0) {
          this.$message.error("文件下载失败！！文件不存在");
        }
        var blob = new Blob([response.data], {
          type: "application/octet-stream;charset=utf-8",
        });

        // let contentDisposition = response.headers['content-disposition'];
        let contentDisposition = response.headers["content-disposition"].split(";");
        let filename = contentDisposition[contentDisposition.length - 1].split("=")[1];
        console.log(filename)
        this.$message.success("文件" + filename + "下载成功！");

        const link = document.createElement("a");
        link.href = window.URL.createObjectURL(blob);
        link.download = filename;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      });
    },
  },
};
</script>